package com.shawn.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shawn.dubbo.mapper.UserMapper;
import com.shawn.model.entity.User;
import com.shawn.model.vo.DistributionVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService //暴露服务
public class UserApiImpl implements UserApi{

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByPhone(String phoneNum) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getMobile,phoneNum);
        return userMapper.selectOne(wrapper);
    }

    @Override
    public Long save(User user) {
        userMapper.insert(user);
        return user.getId();
    }

    @Override
    public void update(User user) {
        userMapper.updateById(user);
    }

    @Override
    public User findById(Long userId) {
        return userMapper.selectById(userId);
    }

    /**
     * 根据环信ID查找用户
     *
     * @param hxId hx id
     * @return {@link User}
     */
    @Override
    public User getUserByHXId(String hxId) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getHxUser,hxId);
        return userMapper.selectOne(wrapper);
    }

    /**
     * 获取当前用户数量
     *
     * @return {@link Integer}
     */
    @Override
    public Integer getUserNum() {
        return userMapper.selectCount(new LambdaQueryWrapper<>());
    }


}
